package library.utils;

import java.awt.Cursor;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import org.apache.commons.io.IOUtils;

/**
 * @author Dmitry
 */
public class Utils {
    private static Font defaultFont;

    public static String getCurrentDirectory() {
        try {
            return new File(Utils.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getPath();
        } catch (URISyntaxException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static Font getDefaultFont() {
        return defaultFont;
    }

    public static void setDefaultFont(Font defaultFont) {
        Utils.defaultFont = defaultFont;
    }

    public static void errorDialog(String message) {
        JOptionPane.showMessageDialog(null, message, "Ошибка", JOptionPane.ERROR);
    }

    public static void log(Throwable ex) {
        ex.printStackTrace();
    }
    
    public static void log(String message) {
        System.out.println(message);
    }

    public static ImageIcon loadIcon(String icon) {
        try {
            String path = "/resources/icons/" + icon;
            BufferedImage image = ImageIO.read(Utils.class.getResourceAsStream(path));
            return new ImageIcon(image);
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static String inputStreamToString(InputStream stream) {
        try {
            return IOUtils.toString(stream, "UTF8");
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void handCursor(JComponent component) {
        component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    public static void defaultCursor(JComponent component) {
        component.setCursor(Cursor.getDefaultCursor());
    }
    
    public static String getStorePath(){
        return "D:/bookCatalog/";
    }
}
